<template>
  <div class="theme-switcher" @click="toggleTheme">
    <div class="toggle-bg" :class="{ 'is-on': isDarkModeComputed }"></div>
    <div class="toggle-thumb">
      <v-icon name="taiyang" width="1rem" height="1rem" class="icon icon-light" />
      <v-icon name="yueliang" width="1rem" height="1rem" class="icon icon-dark" />
    </div>
  </div>
</template>

<script setup>
import { useThemeStore } from '@/store'

const themeStore = useThemeStore()

function toggleTheme() {
  const newMode = themeStore.mode === 'dark' ? 'light' : 'dark'
  themeStore.changeMode(newMode)
}

onMounted(() => {
  themeStore.initTheme()
})

const isDarkModeComputed = computed(() => themeStore.mode === 'dark')
</script>

<style lang="scss" scoped>
.theme-switcher {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: calc(3.5rem + 4px);
  height: calc(1.75rem + 4px);
  border-radius: 5px;
  background-color: var(--td-bg-color-component);
  cursor: pointer;
  position: relative;
}

.theme-switcher .toggle-bg {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 5px;
  background-color: var(--td-bg-color-container);
  position: absolute;
  top: 2px;
  left: 2px;
  transition: all 0.3s ease;
}

.theme-switcher .toggle-bg.is-on {
  left: calc(100% - 1.75rem - 2px);
}

.theme-switcher .toggle-thumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  width: 100%;
  position: relative;
}

.theme-switcher .icon {
  width: 1rem;
  height: 1rem;
  transition: fill 0.3s ease !important;
}

.theme-switcher .icon-light {
  position: absolute;
  left: calc(50% - 1.3rem - 2px);
  color: var(--switch-light-icon-color);
}

.theme-switcher .icon-dark {
  position: absolute;
  right: calc(50% - 1.3rem - 2px);
  color: var(--switch-dark-icon-color);
}

.theme-switcher .toggle-bg.is-on .icon-light {
  fill: var(--td-gray-color-3);
}

.theme-switcher .toggle-bg.is-on .icon-dark {
  fill: var(--td-gray-color-1);
}
</style>
